package linklist;

/**
 * @author Jonny Long
 * @date 2021/2/1 21:23
 */
public class Solution203 {
    public ListNode removeElements(ListNode head, int val) {
        ListNode list = new ListNode(0);
        //为链表添加首节点list，不带头节点list，链表第一个元素为head
        list.next = head;

        ListNode prev = list, curr = head;
        while (curr != null) {
            if (curr.val == val) {
                prev.next = curr.next;
            } else {
                prev = curr;
            }
            curr = curr.next;
        }
        return list.next;
    }
}
